package com.scl.cloud.route;

import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @ClassName ZuulRouteLocator
 * @Description 动态路由
 * @Author shengchenglong
 * @DATE 2019-06-05 10:27
 * @Version 1.0
 */
public class ZuulRouteLocator extends SimpleRouteLocator implements RefreshableRouteLocator {

    private ZuulProperties properties;

    private List<ZuulProperties.ZuulRoute> locateRouteList;

    public ZuulRouteLocator(String servletPath, ZuulProperties properties) {
        super(servletPath, properties);
        this.properties = properties;
    }

    @Override
    public void refresh() {
        doRefresh();
    }

    /**
     * 在该方法加载处理好的动态路由配置
     *
     * @return
     */
    @Override
    protected Map<String, ZuulProperties.ZuulRoute> locateRoutes() {
        // 舍弃配置文件中的配置
        // return super.locateRoutes();
        LinkedHashMap<String, ZuulProperties.ZuulRoute> routesMap = new LinkedHashMap();
        // 加载路由配置
        routesMap.putAll(loadLocateRoute());
        // 优化一下配置，防止路由格式不正确
        LinkedHashMap<String, ZuulProperties.ZuulRoute> realRoutesMap = new LinkedHashMap(routesMap.size());
        routesMap.forEach((k, v) -> {
            String key = k;
            if (!key.startsWith("/")) {
                key = "/".concat(key);
            }
            if (this.properties.getPrefix() != null) {
                key = this.properties.getPrefix().concat(key);
                if (!key.startsWith("/")) {
                    key = "/".concat(key);
                }
            }
            realRoutesMap.put(key, v);
        });
        return realRoutesMap;
    }

    /**
     * 从数据源获取路由配置，由子类实现
     *
     * @return
     */
    public Map<String, ZuulProperties.ZuulRoute> loadLocateRoute() {
        // 验证可用
//        ZuulProperties.ZuulRoute r = new ZuulProperties.ZuulRoute();
//        r.setId("/movie/**");
//        r.setServiceId("microservice-provider-user");
//        r.setPath("/movie/**");
        // 验证可用
        ZuulProperties.ZuulRoute r = new ZuulProperties.ZuulRoute();
        r.setId("/test/index");
        r.setServiceId("microservice-provider-user");
        r.setPath("/test/index");
//        r.setUrl("/index");

        Map<String, ZuulProperties.ZuulRoute> map = new HashMap<>();
        map.put(r.getId(), r);
        return map;
    }



//    @Override
//    public Route getMatchingRoute(String path) {
//        return new Route("/api/test", "/movie/index", null, "",
//                null, new LinkedHashSet<>(),true);
//
//    }
//
//    @Override
//    protected Route getRoute(ZuulProperties.ZuulRoute route, String path) {
//        return new Route("/api/test", "/movie/index", null, "",
//                null, new LinkedHashSet<>(),true);
//    }
}
